require "esi/handler"

module ESI

  class Dispatcher < Mongrel::HttpHandler
    attr_reader :config, :router

    Thread.abort_on_exception = false

    def initialize( root, options = {} )
      super()
      @config = ESI::Config.new(options[:config_file] || "#{root}/config/esi.yml")
      @router = Router.new(YAML.load_file("#{root}/config/routes.yml") )
      if @config.start_invalidator?
        ESI::Invalidator.start( self )
      end
    end
    
    def process(request, response)
      handler = Handler.new(self)
      handler.process(request, response)
    end
  end
end
